package com.bitdf.txing.ware.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.bitdf.txing.common.enume.TxCodeEnume;
import com.bitdf.txing.common.to.SkuHasStockVo;
import com.bitdf.txing.common.utils.PageUtils;
import com.bitdf.txing.common.utils.R;
import com.bitdf.txing.ware.entity.WareSkuEntity;
import com.bitdf.txing.ware.service.WareSkuService;
import com.bitdf.txing.ware.vo.WareSkuLockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 商品库存
 *
 * @author lizhiwei
 * @email sunlightcs@gmail.com
 * @date 2022-05-17 00:04:30
 */
@RestController
@RequestMapping("${txing.request.path.ware}/sku")
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;


    /**
     * 为订单锁定库存
     */
    @SaCheckLogin
    @PostMapping("/lock/order")
    public R orderLockStock(@RequestBody WareSkuLockVo vo) {
        try{
            Boolean stock = wareSkuService.orderLockStock(vo);
            return R.ok();
        }catch (Exception e){
            return R.error(TxCodeEnume.WARE_NOT_STOCK_EXCEPTION);
        }
    }

    /**
     * 查询sku是否有库存
     * @param skuIds
     * @return
     */
    @PostMapping("/hasstock")
    public R getSkusHasStock(@RequestBody List<Long> skuIds)
    {
        List<SkuHasStockVo> vos = wareSkuService.getSkusHasStock(skuIds);
        return R.ok().setData(vos);
    }

    /**
     * 列表
     */
    @SaCheckLogin
    @SaCheckPermission("mall:ware:sku:list")
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = wareSkuService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @SaCheckLogin
    @SaCheckPermission("mall:ware:sku:info:get")
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		WareSkuEntity wareSku = wareSkuService.getById(id);

        return R.ok().put("wareSku", wareSku);
    }

    /**
     * 保存
     */
    @SaCheckLogin
    @SaCheckPermission("mall:ware:sku:save")
    @RequestMapping("/save")
    public R save(@RequestBody WareSkuEntity wareSku){
		wareSkuService.save(wareSku);

        return R.ok();
    }

    /**
     * 修改
     */
    @SaCheckLogin
    @SaCheckPermission("mall:ware:sku:update")
    @RequestMapping("/update")
    public R update(@RequestBody WareSkuEntity wareSku){
		wareSkuService.updateById(wareSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @SaCheckLogin
    @SaCheckPermission("mall:ware:sku:delete")
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		wareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
